<?php
namespace app\realname\controller;

use tp5_lib\common\controller\TestBaseController;
use tp5_lib\realname\model\IDCardModel;

class TestController extends TestBaseController
{
    public function index()
    {
        return $this->fetch();
    }
    public function doUpload()
    {
        $data=input();
        $data["time"]=time();
        
        $user=new UserModel();
        if(!$user->where(["id"=>$data["user_id"]])->find()){
            $this->error("用户不存在");
        }

        $model=new IDCardModel();
        if(!$model->check($data["no"])){
            $this->error("请输入正确的身份证号");
        }

        $upload_dir=env("root_path").'/public/uploads';
        $files = $this->request->file();

        if(isset($files["frontage"])){
            if($info=$files["frontage"]->move($upload_dir)){
                $data["frontage"]=$info->getSaveName();
            }
            else{
                $this->error($files["frontage"]->getError());
            }
        }

        if(isset($files["backside"])){
            if($info=$files["backside"]->move($upload_dir)){
                $data["backside"]=$info->getSaveName();
            }
            else{
                $this->error($files["backside"]->getError());
            }
        }

        if(!$model->db()->insert($data)){
            $this->error("保存失败");
        }
        $this->success();
    }
}
